from rest_framework.exceptions import ValidationError
from rest_framework.views import exception_handler
from utils.response import APIResponse
from utils.logger import get_logger

logger = get_logger()


def custom_exception_handler(exc, context):
    res = exception_handler(exc, context)
    if not res:
        # 出了异常
        response = APIResponse(code=999, msg='系统错误')
        logger.error('服务端错误，错误原因是：%s,出错的view是：%s，请求地址是：%s' % (
            str(exc),
            str(context['view']),
            context['request'].get_full_path()
        )
                     )
    else:
        response = APIResponse(code=999, msg=res.data)
    return response
    # response = exception_handler(exc, context)
    # print(response)
    # if isinstance(exc, ValidationError):
    #     response.data = {"fields_errors": "response.data"}
    # else:
    #
    #     response.data = {"none_fields_errors": "111response.data"}
    # return response
